/**
 * tv视频详情播放页面
 * @author tanggf
*/
$(function(){
	show_detail.run();
});

var show_detail = {	
		run: function(){
			this.__init__();			
			this._start_();
			this._bind_events();
		},
		
		__init__: function(){
			this.contextPath = $("#contextPath").val() || "";
			this.showId = $("#showId").val();
			this.type = $("#type").val();		
			this.genres = encodeURIComponent($("#genres").val());
			this.isShow = (this.type == 'show');
			this.isGame = (this.type == 'game');
			this.itemUrl = $("#itemUrl").val();
			this.title = $("#title").val() || '';
			this.interval = null;
		},
		
		_start_: function(){
			share.run();
			
			this._get_episodes_detail();
			this._get_episodes_playlist();
			this._get_recent_releases();			
		},
		
		_get_recent_releases: function(){						
			var url = this.contextPath + "/video/tv/recent_" + this.type;
			if( ! this.isGame){
				url  += "?genres=" + this.genres;		
			}
			$.get(url, function(result){
				$("#new_release_div").html(result);
			});
		},
		
		_get_episodes_detail: function(){			
			if( ! this.isGame){
				var url = this.contextPath + "/video/tv/episodes_detail?showId=" + this.showId;
				$.get(url, function(result){
					$("#show_episodes_div").html(result);
					show_detail.__bind_slide_down_events();
				});
			}			
		},
		
		_get_episodes_playlist: function(){
			if(this.isShow){
				var url = this.contextPath + "/video/tv/episodes_playlist?showId=" + this.showId;
				$.get(url, function(result){
					$("#epidoses_playlist_div").html(result);
					$("#epidoses_playlist_div > ul > li > a").click(function(){
						var title = $(this).attr("title");
						_gaq.push(['_trackEvent', 'TVShow', 'Play', title]);		
					});
				});
			}			
		},
		
		//显示隐藏season list
		__bind_slide_down_events: function(){
			$("#show_episodes_div > h4.slidedown").click(function(){				
				$(this).next().slideToggle();				
			});
		},
		
		_bind_events: function(){
			//vmenu
			$("#category_select_div").vmenu();
			
			//play now btn
			$("#play_now_btn").click(function(){
				if(show_detail.isGame){
					var title = $(this).attr('title');
					_gaq.push(['_trackEvent', 'Game', 'Play', title]);
					window.open(show_detail.itemUrl);	
				}else if(show_detail.isShow){
					var $div = $("#epidoses_playlist_div");
					$div.slideToggle();	
					$("#epidoses_playlist_div > span.play-close").click(function(){
						$div.slideUp();
					});
				}else{
					var title = $(this).attr('title');
					_gaq.push(['_trackEvent', 'Episode', 'Play', title]);
					window.open(show_detail.itemUrl);		
				}			
			});			
			
			//favortie btn
			$("#favorite_btn").click(function(){
				alert('favorite');
			});
			
			//download btn
			$("#download_btn").click(function(){
				alert('download');
			});	
			
			$("#fb-comments").attr("data-href", location.href);
		}
};

var share = {
	run: function(){
		//share btn
		$("#share_btn").click(function(){
			$("#share_fb_div").slideToggle();
			$("#share_fb_div > span.share-close").click(function(){
				$("#share_fb_div").slideUp();
			});
		});		
		
		$("#share_input").val(location.href);
		
		$("#fb_share_a").click(function(){
			var url = $("#share_input").val();
			var fbUrl = "https://www.facebook.com/sharer.php?u=" + encodeURIComponent(url);
			_gaq.push(['_trackEvent', 'Show', 'fb_share', url]);	
			window.open(fbUrl);
		});
		
		$("#tw_share_a").click(function(){
			var url = $("#share_input").val();
			var twUrl = "https://twitter.com/share?url=" +encodeURIComponent(url);
			_gaq.push(['_trackEvent', 'Show', 'twitter_share', url]);				
			window.open(twUrl);
		});
	}
};